/**
 * This is a class to display the initial configuration
 */
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author 522sk
 * @version $Revision: 1.0 $
 */
public class InitialConfigurationScreen extends JPanel{

	/**
	 * Field SIBAISI.
	 * (value is 440)
	 */
	private static final int SIBAISIAA = 440;

	/**
	 * Field SIBAIQI.
	 * (value is 470)
	 */
	private static final int SIBAIQIA = 470;

	/**
	 * Field ERBAISI.
	 * (value is 240)
	 */
	private static final int ERBAISI = 240;

	/**
	 * Field LIUSHI.
	 * (value is 60)
	 */
	private static final int LIUSHI = 60;

	/**
	 * Field SANBAI.
	 * (value is 300)
	 */
	private static final int SANBAI = 300;

	/**
	 * Field SIBAIER.
	 * (value is 420)
	 */
	private static final int SIBAIER = 420;

	/**
	 * Field SIBAIYI.
	 * (value is 410)
	 */
	private static final int SIBAIYI = 410;

	/**
	 * Field SHILIU.
	 * (value is 16)
	 */
	private static final int SHILIU = 16;

	/**
	 * Field SANSHI.
	 * (value is 30)
	 */
	private static final int SANSHI = 30;

	/**
	 * Field SIBAISAN.
	 * (value is 430)
	 */
	private static final int SIBAISAN = 430;

	/**
	 * Field SIBAISANSHIBA.
	 * (value is 438)
	 */
	private static final int SIBAISANSHIBA = 438;

	/**
	 * Field SIBAISISHIER.
	 * (value is 442)
	 */
	private static final int SIBAISISHIER = 442;

	/**
	 * Field ERBAIWU.
	 * (value is 250)
	 */
	private static final int ERBAIWU = 250;

	/**
	 * Field SIBAISISHIWU.
	 * (value is 455)
	 */
	private static final int SIBAISISHIWU = 455;

	/**
	 * Field SANBAIQI.
	 * (value is 370)
	 */
	private static final int SANBAIQIA = 370;

	/**
	 * Field SANBAISI.
	 * (value is 340)
	 */
	private static final int SANBAISIAA = 340;

	/**
	 * Field WUBAIWU.
	 * (value is 550)
	 */
	private static final int WUBAIWU = 550;

	/**
	 * Field YIBAIER.
	 * (value is 120)
	 */
	private static final int YIBAIER = 120;

	/**
	 * Field ERBAIBA.
	 * (value is 280)
	 */
	private static final int ERBAIBA = 280;

	/**
	 * Field YIBAIWU.
	 * (value is 150)
	 */
	private static final int YIBAIWU = 150;

	/**
	 * Field SIBAISANSHIWU.
	 * (value is 435)
	 */
	private static final int SIBAISANSHIWU = 435;

	/**
	 * Field SANBAIYI.
	 * (value is 310)
	 */
	private static final int SANBAIYI = 310;

	/**
	 * Field ERBAIER.
	 * (value is 220)
	 */
	private static final int ERBAIER = 220;

	/**
	 * Field YIBAIJIU.
	 * (value is 190)
	 */
	private static final int YIBAIJIUA = 190;

	/**
	 * Field YIBAILIU.
	 * (value is 160)
	 */
	private static final int YIBAILIU = 160;

	/**
	 * Field QISHI.
	 * (value is 70)
	 */
	private static final int QISHI = 70;

	/**
	 * Field WUBAI.
	 * (value is 500)
	 */
	private static final int WUBAI = 500;

	/**
	 * Field BABAI.
	 * (value is 800)
	 */
	private static final int BABAI = 800;

	/**
	 * Field YIBAISAN.
	 * (value is 130)
	 */
	private static final int YIBAISAN = 130;

	/**
	 * Field ERBAI.
	 * (value is 200)
	 */
	private static final int ERBAI = 200;

	/**
	 * Field SISHI.
	 * (value is 40)
	 */
	private static final int SISHI = 40;

	/**
	 * Field SIBAI.
	 * (value is 400)
	 */
	private static final int SIBAI = 400;

	/**
	 * Field SIBAIWU.
	 * (value is 450)
	 */
	private static final int SIBAIWU = 450;

	/**
	 * Field SHI.
	 * (value is 10)
	 */
	private static final int SHI = 10;

	/**
	 * Field YIBAI.
	 * (value is 100)
	 */
	private static final int YIBAI = 100;

	/**
	 * Field ERSHI.
	 * (value is 20)
	 */
	private static final int ERSHI = 20;

	/**
	 * Field errorMsgSP.
	 */
	/**
	 * Field errorMsgName.
	 */
	/**
	 * Field skillPoints.
	 */
	/**
	 * Field title.
	 */
	public JLabel title, skillPoints, errorMsgName, errorMsgSP;

	/**
	 * Field playerNameT.
	 */
	public JTextField playerNameT;

	/**
	 * Field difficultyL.
	 */
	/**
	 * Field playerNameL.
	 */
	public JLabel playerNameL, difficultyL;

	/**
	 * Field fighterL.
	 */
	/**
	 * Field engineerL.
	 */
	/**
	 * Field traderL.
	 */
	/**
	 * Field pilotL.
	 */
	public JLabel pilotL, traderL, engineerL, fighterL;

	/**
	 * Field loadGame.
	 */
	/**
	 * Field engineerMinus.
	 */
	/**
	 * Field engineerAdd.
	 */
	/**
	 * Field traderMinus.
	 */
	/**
	 * Field traderAdd.
	 */
	/**
	 * Field fighterMinus.
	 */
	/**
	 * Field fighterAdd.
	 */
	/**
	 * Field pilotMinus.
	 */
	/**
	 * Field pilotAdd.
	 */
	/**
	 * Field next.
	 */
	public JButton next, pilotAdd, pilotMinus, fighterAdd, fighterMinus;
	
	/**
	 * Field traderAdd.
	 */
	/**
	 * Field loadGame.
	 */
	/**
	 * Field engineerMinus.
	 */
	/**
	 * Field engineerAdd.
	 */
	/**
	 * Field traderMinus.
	 */
	public JButton traderMinus, engineerAdd, engineerMinus, loadGame, traderAdd;

	/**
	 * Field impossibleB.
	 */
	/**
	 * Field hardB.
	 */
	/**
	 * Field normalB.
	 */
	/**
	 * Field easyB.
	 */
	/**
	 * Field beginnerB.
	 */
	public JRadioButton beginnerB, easyB, normalB, hardB, impossibleB;

	/**
	 * Field level.
	 */
	public int level;

	/**
	 * Field diff.
	 */
	public ButtonGroup diff;
	
	/**
	 * Constructor for InitialConfigurationScreen.
	 */
	public InitialConfigurationScreen(){
		   final ImagePanel pic = 
				   new ImagePanel(new ImageIcon("background1.jpg").getImage());
		   new GameInfo();
		   playerNameT=new JTextField(ERSHI);
		   skillPoints=new JLabel("SkillPoints");
		   title=new JLabel("SpaceTrader");
		   errorMsgName=new JLabel("");
		   errorMsgSP=new JLabel("");
		  
		   playerNameL=new JLabel("Player Name:");
		   difficultyL=new JLabel("Please choose a difficulty level:");
		   final ButtonGroup diff=new ButtonGroup();
		   next=new JButton("Next");
		   beginnerB=new JRadioButton("Beginner");
		   easyB=new JRadioButton("Easy");
		   normalB=new JRadioButton("Normal");
		   hardB=new JRadioButton("Hard");
		   impossibleB=new JRadioButton("Impossible");
		   diff.add(beginnerB);
		   diff.add(easyB);
		   diff.add(normalB);
		   diff.add(hardB);
		   diff.add(impossibleB);
		   final DifficultyListener diffListener=new DifficultyListener();
		   pilotL=new JLabel("Pilot Points: " + GameInfo.getPilotSP());
		   traderL=new JLabel("Trader Points: " + GameInfo.getTraderSP());
		   fighterL=new JLabel("Fighter Points: " + GameInfo.getFighterSP());
		   engineerL=new JLabel("Engineer Points: " + GameInfo.getEngineerSP());
		   pilotAdd=new JButton(">");
		   pilotMinus=new JButton("<");
		   fighterAdd=new JButton(">");
		   fighterMinus=new JButton("<");
		   traderAdd=new JButton(">");
		   traderMinus=new JButton("<");
		   engineerAdd=new JButton(">");
		   engineerMinus=new JButton("<");
		   loadGame=new JButton("Load Game");
		   loadGame.addActionListener(new LoadListener());
		   pilotAdd.addActionListener(new PilotAddListener());
		   pilotMinus.addActionListener(new PilotMinusListener());
		   fighterAdd.addActionListener(new FighterAddListener());
		   fighterMinus.addActionListener(new FighterMinusListener());
		   traderAdd.addActionListener(new TraderAddListener());
		   traderMinus.addActionListener(new TraderMinusListener());
		   engineerAdd.addActionListener(new EngineerAddListener());
		   engineerMinus.addActionListener(new EngineerMinusListener());
		   
		   beginnerB.addActionListener(diffListener);
		   easyB.addActionListener(diffListener);
		   normalB.addActionListener(diffListener);
		   hardB.addActionListener(diffListener);
		   impossibleB.addActionListener(diffListener);
		   normalB.setSelected(true);
		   GameInfo.setDifficulty("Normal");
		   
		   next.addActionListener(new NextListener());
		   
		   this.setLayout(null);
		   add(loadGame);
		   loadGame.setBounds(BABAI, SHI, YIBAI, ERSHI);
		   add(title);
		   title.setBounds(SIBAIWU, SHI, YIBAI, ERSHI);
		   add(playerNameL);
		   playerNameL.setBounds(SIBAI, SISHI, YIBAI, ERSHI);
		   add(playerNameT);
		   playerNameT.setBounds(WUBAI, SISHI, YIBAI, ERSHI);
		   add(difficultyL);
		   difficultyL.setBounds(SIBAI, QISHI, ERBAI, ERSHI);
		   add(beginnerB);
		   beginnerB.setBounds(SIBAIWU, YIBAI, YIBAI, ERSHI);
		   add(easyB);
		   easyB.setBounds(SIBAIWU, YIBAISAN, YIBAI, ERSHI);
		   add(normalB);
		   normalB.setBounds(SIBAIWU, YIBAILIU, YIBAI, ERSHI);
		   add(hardB);
		   hardB.setBounds(SIBAIWU, YIBAIJIUA, YIBAI, ERSHI);
		   add(impossibleB);
		   impossibleB.setBounds(SIBAIWU, ERBAIER, YIBAI, ERSHI);
           add(skillPoints);
           skillPoints.setBounds(SIBAI, ERBAIWU, YIBAIWU, ERSHI);
		   add(pilotAdd);
		   add(pilotMinus);
		   add(pilotL);
		   pilotMinus.setBounds(SIBAI, ERBAIBA, ERSHI, ERSHI);
		   pilotL.setBounds(SIBAISISHIER, ERBAIBA, YIBAI, ERSHI);
		   pilotAdd.setBounds(WUBAIWU, ERBAIBA, ERSHI, ERSHI);
		   add(fighterAdd);
		   add(fighterMinus);
		   add(fighterL);
		   fighterMinus.setBounds(SIBAI, SANBAIYI, ERSHI, ERSHI);
		   fighterL.setBounds(SIBAISANSHIWU, SANBAIYI, YIBAIER, ERSHI);
		   fighterAdd.setBounds(WUBAIWU, SANBAIYI, ERSHI, ERSHI);
		   add(traderAdd);
		   add(traderMinus);
		   add(traderL);
		   traderMinus.setBounds(SIBAI, SANBAISIAA, ERSHI, ERSHI);
		   traderL.setBounds(SIBAISANSHIBA, SANBAISIAA, YIBAI, ERSHI);
		   traderAdd.setBounds(WUBAIWU, SANBAISIAA, ERSHI, ERSHI);
		   add(engineerAdd);
		   add(engineerMinus);
		   add(engineerL);
		   engineerMinus.setBounds(SIBAI, SANBAIQIA, ERSHI, ERSHI);
		   engineerL.setBounds(SIBAISAN, SANBAIQIA, YIBAIER, ERSHI);
		   engineerAdd.setBounds(WUBAIWU, SANBAIQIA, ERSHI, ERSHI);
		   add(next);
		   next.setBounds(SIBAISISHIWU, SIBAIYI, LIUSHI, SANSHI);
		   add(errorMsgName);
		   errorMsgName.setBounds(SIBAIER, SIBAISIAA, ERBAISI, SANSHI);
		   add(errorMsgSP);
		   errorMsgSP.setBounds(SANBAI, SIBAIQIA, WUBAI, SANSHI);
		   add(pic);
		   }

	/**
	 * Method isSkillValid.
	
	 * @return boolean */
	private boolean isSkillValid(){
		if (GameInfo.getPilotSP() < 0) return false;
		if (GameInfo.getTraderSP() < 0) return false;
		if (GameInfo.getFighterSP() < 0) return false;
		if (GameInfo.getEngineerSP() < 0) return false;
		return (GameInfo.getPilotSP() + GameInfo.getTraderSP() +
				GameInfo.getFighterSP() + GameInfo.getEngineerSP() <= SHILIU);
	}

	/**
	 * Method isSkillComplete
	 * @return boolean */
	private boolean isSkillComplete(){
		if (!isSkillValid()) return false;
		return (GameInfo.getPilotSP() + GameInfo.getTraderSP() +
				GameInfo.getFighterSP() + GameInfo.getEngineerSP() == SHILIU);
	}

	/**
	 * @author 522sk
	 */
	private class DifficultyListener implements ActionListener{
        /**
         * Method toString.
        
         * @return String */
        @Override
		public String toString() {
			return "DifficultyListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
        
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override
        public void actionPerformed(ActionEvent event){
        	if (event.getSource().equals(beginnerB)) {
        		GameInfo.setDifficulty("Beginner");
        	}else if (event.getSource().equals(easyB)) {
        		GameInfo.setDifficulty("Easy");
        	}else if (event.getSource().equals(normalB)) {
        		GameInfo.setDifficulty("Normal");
        	}else if (event.getSource().equals(hardB)) {
        		GameInfo.setDifficulty("Hard");
        	}else {
        		GameInfo.setDifficulty("Impossible");
        	}
        	
        }
    }
    
    /**
     * @author 522sk
     */
    private class PilotAddListener implements ActionListener{

		/**
		 * Method toString.
		
		 * @return String */
		@Override
		public String toString() {
			return "PilotAddListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
        
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override public void actionPerformed(ActionEvent event){
        	GameInfo.setPilotSP(GameInfo.getPilotSP() + 1);
        	if (isSkillValid()) {
        		pilotL.setText("Pilot Points: " + GameInfo.getPilotSP());
        		return;
        	}
        	GameInfo.setPilotSP(GameInfo.getPilotSP() - 1);
        }
    }

    /**
     * @author 522sk
     */
    private class PilotMinusListener implements ActionListener{
        /**
         * Method toString.
        
         * @return String */
        @Override
		public String toString() {
			return "PilotMinusListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
        
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override 
        public void actionPerformed(ActionEvent event){
        	GameInfo.setPilotSP(GameInfo.getPilotSP() - 1);
        	if (isSkillValid()) {
        		pilotL.setText("Pilot Points: " + GameInfo.getPilotSP());
        		return;
        	}
        	GameInfo.setPilotSP(GameInfo.getPilotSP() + 1);
        }
    }

    /**
     * @author 522sk
     */
    private class TraderAddListener implements ActionListener{
        /**
         * Method toString.
        
         * @return String */
        @Override
		public String toString() {
			return "TraderAddListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override 
        public void actionPerformed(ActionEvent event){
        	GameInfo.setTraderSP(GameInfo.getTraderSP() + 1);
        	if (isSkillValid()) {
        		traderL.setText("Trader Points: " + GameInfo.getTraderSP());
        		return;
        	}
        	GameInfo.setTraderSP(GameInfo.getTraderSP() - 1);
        }
    }

    /**
     * @author 522sk
     */
    private class TraderMinusListener implements ActionListener{
        /**
         * Method toString.
        
         * @return String */
        @Override
		public String toString() {
			return "TraderMinusListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
        
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override 
        public void actionPerformed(ActionEvent event){
        	GameInfo.setTraderSP(GameInfo.getTraderSP() - 1);
        	if (isSkillValid()) {
        		traderL.setText("Trader Points: " + GameInfo.getTraderSP());
        		return;
        	}
        	GameInfo.setTraderSP(GameInfo.getTraderSP() + 1);
        }
    }

    /**
     * @author 522sk
     */
    private class FighterAddListener implements ActionListener{
        /**
         * Method toString.
        
         * @return String */
        @Override
		public String toString() {
			return "FighterAddListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
        
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override
        public void actionPerformed(ActionEvent event){
        	GameInfo.setFighterSP(GameInfo.getFighterSP() + 1);
        	if (isSkillValid()) {
        		fighterL.setText("Fighter Points: " + GameInfo.getFighterSP());
        		return;
        	}
        	GameInfo.setFighterSP(GameInfo.getFighterSP() - 1);
        }
    }

    /**
     * @author 522sk
     */
    private class FighterMinusListener implements ActionListener{
        /**
         * Method toString.
        
         * @return String */
        @Override
		public String toString() {
			return "FighterMinusListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
        
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override
        public void actionPerformed(ActionEvent event){
        	GameInfo.setFighterSP(GameInfo.getFighterSP() - 1);
        	if (isSkillValid()) {
        		fighterL.setText("Fighter Points: " + GameInfo.getFighterSP());
        		return;
        	}
        	GameInfo.setFighterSP(GameInfo.getFighterSP() + 1);
        }
    }

    /**
     * @author 522sk
     */
    private class EngineerAddListener implements ActionListener{
        /**
         * Method toString.
        
         * @return String */
        @Override
		public String toString() {
			return "EngineerAddListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
        
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override 
        public void actionPerformed(ActionEvent event){
        	GameInfo.setEngineerSP(GameInfo.getEngineerSP() + 1);
        	if (isSkillValid()) {
        		engineerL.setText("Engineer Points: " + GameInfo.getEngineerSP());
        		return;
        	}
        	GameInfo.setEngineerSP(GameInfo.getEngineerSP() - 1);
        }
    }

    /**
     * @author 522sk
     */
    private class EngineerMinusListener implements ActionListener{
        /**
         * Method toString.
        
         * @return String */
        @Override
		public String toString() {
			return "EngineerMinusListener []";
		}

		/**
         * Method actionPerformed.
         * @param event ActionEvent
        
         * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
        @Override
        public void actionPerformed(ActionEvent event){
        	GameInfo.setEngineerSP(GameInfo.getEngineerSP() - 1);
        	if (isSkillValid()) {
        		engineerL.setText("Engineer Points: " + GameInfo.getEngineerSP());
        		return;
        	}
        	GameInfo.setEngineerSP(GameInfo.getEngineerSP() + 1);
        }
    }

    /**
     * @author 522sk
     */
    private class NextListener implements ActionListener{
		/**
		 * Method toString.
		
		 * @return String */
		@Override
		public String toString() {
			return "NextListener []";
		}

		/**
		 * Method actionPerformed.
		 * @param arg0 ActionEvent
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		@Override 
		public void actionPerformed(ActionEvent arg0) {
			if (!isSkillComplete() || playerNameT.getText().equals("")){
				if (playerNameT.getText().equals("")){
					errorMsgName.setText("Please enter your name.");
				}else {
					errorMsgName.setText("");
				}
				if (!isSkillComplete()){
					errorMsgSP.setText("Please set the skill points total to 16.");
				}else {
					errorMsgSP.setText("");
				}
				// we should not go next
			}else {
				GameInfo.setName(playerNameT.getText());
				errorMsgName.setText("");
				errorMsgSP.setText("");
				// go to the next scenes
				UniverseScreen.start();
			}
		}
    	
    }
}
    
	    

